package com.android.miniskirt.service;

import java.util.ArrayList;
import java.util.HashMap;

import com.android.miniskirt.R;



import android.app.Service;
import android.content.Intent;
import android.database.Cursor;
import android.os.Binder;
import android.os.IBinder;
import android.provider.ContactsContract;
import android.util.Log;


public class getcontacts extends Service {
	private static final String TAG = "APP";
	public final static String ACTION = "start contacts service";
	public boolean isContacts = false;
	private final IBinder mBinder = new contactsBinder();
	public class contactsBinder extends Binder {
		public getcontacts getService() {
			return getcontacts.this;
		}
	}
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return mBinder;
	}
	
	//read mobile contacts
	public ArrayList<HashMap<String, Object>> getData() { 
	 	   ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();       
		        Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
		        while (cursor.moveToNext())   
		        {    
		         HashMap<String, Object> map = new HashMap<String, Object>();
		         String phoneName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
		         if(phoneName.charAt(0) != '0' && phoneName.charAt(0) != '1' && phoneName.charAt(0) != '2' && phoneName.charAt(0) != '3' && phoneName.charAt(0) != '4' &&  phoneName.charAt(0) != '5' && phoneName.charAt(0) != '6' && phoneName.charAt(0) != '7' && phoneName.charAt(0) != '8' && phoneName.charAt(0) != '9')
		         	{
		        	map.put("img", R.drawable.icon);
		        	 map.put("ItemTitle", phoneName);
		             String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));  
		                String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));  
		               
		                if (hasPhone.compareTo("1") == 0)
		                {  
		                    Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);       
		                    while (phones.moveToNext())   
		                    {     
		                     String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));      
		                        String phoneTpye = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE));      
		                      
		                        map.put("ItemText", phoneNumber); 
		                       
		                        Log.d(TAG,"testNum="+ phoneNumber + "type:"+phoneTpye);
		                    }       
		                    phones.close();      
		                }      
		                Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null);  
		                while (emails.moveToNext())   
		                {                   
		                    String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));  
		                    String emailType = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));      

		                    Log.d(TAG,"testNum="+ emailAddress + "type:"+emailType);
		                }      
		                emails.close();
		               
		                listItem.add(map);
		         	}
		         
		        }
		       cursor.close();
		       isContacts = true;
		       return listItem;
			}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
//		getData();
//		if(isContacts == true)
//			sendBroadcast(new Intent(ACTION));
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		this.stopSelf();
	}

	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
//		return super.onUnbind(intent);
		this.stopSelf();
		return true;
	}
}